package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import org.json.JSONArray
import org.json.JSONException;

/**
 *
 * @Title: ApiBPCheck* @Description:小程序登录接口--内部接口
 * 验证客户是否属于客服系统的客户
 * @author: chieh* @date 2020-11-11 21:28:00
 *
 */
public class ApiBPCheck implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(ApiBPCheck.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start ApiBPCheck 开始-----------------");

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String reJson = null;
        long endTime = 0;

        //获取参数openid
        def openid = request.getParameter("openid");
        log.debug("openid" + openid);

        JSONArray resArray = new JSONArray();
        JSONObject resObject = new JSONObject();
        try {
            if (request.getParameter("openid") == null) {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"验证失败(参数输入异常),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }

            String selectSQL = "select wechatid as wechatid,customername as customername,mobile as mobile from customer where wechatid=? order by createddate";
            List<Map<String, Object>> custresult = jt.queryForList(selectSQL, openid);

            String wechatid = null;
            String customername = null;
            String mobile = null;
            int count = custresult.size();
            if (count > 0) {
                log.debug("count：" + count);
                wechatid = custresult.get(0).get("wechatid");
                customername = custresult.get(0).get("customername");
                mobile = custresult.get(0).get("mobile");

                resObject.put("openid", wechatid);
                resObject.put("customername", customername);
                resObject.put("mobile", mobile);

                resArray.put(resObject);

                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"验证成功，调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray + "}";
            } else {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"2\",\"resultmessage\":\"验证未找到，调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
            }
        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"验证异常，接口调用处理失败,请检查确认入参有效且合法\",\"data\":[]}";

        }
        return reJson;

    }
}